package com.example.demos.service.impl;

import com.example.demos.dto.ReturnOrderDetailsDTO;
import com.example.demos.dto.ReturnOrderDetailsListDTO;
import com.example.demos.dto.ReturnOrdersBySupplierNameDTO;
import com.example.demos.entity.*;
import com.example.demos.mapper.ReturnOrdersMapper;
import com.example.demos.service.ReturnOrdersService;
import com.example.demos.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReturnOrdersServiceImpl implements ReturnOrdersService {
    @Autowired
    private ReturnOrdersMapper returnOrdersMapper;

    @Override
    public PageBean<List<Store>> selectStore() {
        int count = returnOrdersMapper.selectStoreSum();
        List<Store> list = returnOrdersMapper.selectStoreList();
        return new PageBean<>(count, list);
    }

    @Override
    public PageBean<List<ReturnOrders>> select(String state, Integer page, Integer rows) {
        Page<ReturnOrders> ordersPage = PageHelper.startPage(page, rows);
        returnOrdersMapper.select(state);
        PageBean<List<ReturnOrders>> pageBean = new PageBean<>(ordersPage.getTotal(), ordersPage.getResult());
        return pageBean;
    }

    @Override
    public int addReturnOrders(ReturnOrderDetailsListDTO returnOrderDetailsListDTO, HttpSession session) {
        ReturnOrders returnOrders = new ReturnOrders();
        returnOrders.setCreateTime(LocalDateTime.now());
        returnOrders.setCreater(session.getAttribute("username").toString());
        returnOrders.setType("1");
        returnOrders.setSupplierUuid(returnOrderDetailsListDTO.getSupplieruuid());
        List<ReturnOrderDetailsDTO> returnOrderDetails = returnOrderDetailsListDTO.getReturnOrderDetails();
        double money = 0;
        for (ReturnOrderDetailsDTO returnOrderDetail : returnOrderDetails) {
            if (returnOrderDetail.getMoney() == null || returnOrderDetail.getNum() == null)
                return -1;
            money += returnOrderDetail.getMoney().doubleValue();
        }
        returnOrders.setTotalMoney(new BigDecimal(money));
        returnOrders.setState("0");
        returnOrdersMapper.addReturnOrders(returnOrders);

        returnOrderDetails.stream().forEach(e -> returnOrdersMapper.addReturnOrdersDetail(e, returnOrders.getUuid(), returnOrders.getState()));

        return 1;
    }

    @Override
    public int doCheck(Integer id, HttpSession session) {
        ReturnOrders returnOrders = new ReturnOrders();
        returnOrders.setChecker(session.getAttribute("username").toString());
        returnOrders.setCheckTime(LocalDateTime.now());
        return returnOrdersMapper.doCheck(id, returnOrders);
    }

    @Override
    public int doInStore(BigInteger returnOrdersDetailUuid, BigInteger storeUuid, HttpSession session) {
        //首先查询 订单所有详情 根据suid 进行出库 出库完成后修改state为1
        //遍历同一订单下 所有订单详情 确保state是否都为1 如果都为1 修改endtime end 并改状态码为3
        ReturnOrderDetail returnOrderDetail = returnOrdersMapper.selectReturnOrderDetail(returnOrdersDetailUuid);
        returnOrderDetail.setEndTime(LocalDateTime.now());
        returnOrderDetail.setEnder(session.getAttribute("username").toString());
        returnOrderDetail.setStoreUuid(storeUuid);
        returnOrdersMapper.updateReturnOrderDetailEnd(returnOrdersDetailUuid, returnOrderDetail);
        //对仓库内存进行操作
        returnOrdersMapper.updateStoreDetailByReturnOrderDetail(returnOrderDetail);
        //将操作存到另一个表中
        //根据username 找到emp_id
        int empId = returnOrdersMapper.selectEmpIdByUsername(session.getAttribute("username").toString());
        returnOrdersMapper.insertStoreOper(empId, LocalDateTime.now(), storeUuid,
                returnOrderDetail.getGoodsUuid(), returnOrderDetail.getNum());
        int count = returnOrdersMapper.selectReturnOrderDetailByReturnOrderUuid(returnOrderDetail.getReturnOrdersUuid());
        if (count == 0) {
            returnOrdersMapper.updateReturnOrderByCount(LocalDateTime.now(),
                    session.getAttribute("username").toString(),
                    returnOrderDetail.getReturnOrdersUuid());
        }
        return 1;
    }

    @Override
    public PageBean<List<ReturnOrdersBySupplierNameDTO>> selectReturnListByPage(Integer page, Integer rows) {
        Page<ReturnOrdersBySupplierNameDTO> returnOrdersPage = PageHelper.startPage(page, rows);
        returnOrdersMapper.selectReturnListByPage();
        PageBean<List<ReturnOrdersBySupplierNameDTO>> listPageBean = new PageBean<>(returnOrdersPage.getTotal(),
                returnOrdersPage.getResult());
        return listPageBean;

    }


}
